//https://www.bilibili.com/video/BV1d54y1f7B3/?spm_id_from=333.337.search-card.all.click&vd_source=d6fe65f095f01f05591e89d810720ccc
// 2024-10-18


public class P009_数组中三个数的最大乘积_线性扫描 {

    public static void main(String[] args) {
        System.out.println(fun(new int[]{1,2,3,4,5,6}));
    }

    private static int fun(int[] nums) {
        int bestmax = Integer.MIN_VALUE;
        int secondmax = Integer.MIN_VALUE;
        int thirdmax = Integer.MIN_VALUE;

        int bestmin = Integer.MAX_VALUE;
        int secondmin = Integer.MAX_VALUE;

        for (int x : nums) {

            // 注意这些判断逻辑

            if (x < bestmin) {
                secondmin = bestmin;
                bestmin = x;
            }else if (x < secondmin) {
                secondmin = x;
            }

            if (x > bestmax) {
                thirdmax = secondmax;
                secondmax = bestmax;
                bestmax = x;
            }else if (x > secondmax) {
                thirdmax = secondmax;
                secondmax = x;
            }else if (x > thirdmax) {
                thirdmax = x;
            }

        }

        //三正或者两负一正
        return Math.max(bestmax * secondmax * thirdmax,bestmax * bestmin * secondmin);

    }

}
